package com.example.newspapersmanageos.Service;

import com.example.newspapersmanageos.Entity.Page;
import com.example.newspapersmanageos.Entity.User;
import com.example.newspapersmanageos.Mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class LoginService {
    @Autowired
    UserMapper userMapper;

    //登陆
    public String login(User user) {
        User u = userMapper.getUserByInfo(user);
        if (u==null){
            return "用户不存在";
        }
        return "登陆成功";
    }

    //注册
    public String register(User user) {
        String result = "注册成功";
        List<User> u = userMapper.getUserByAcNumber(user);
        if (u.size() != 0) {
            result = "用户已存在";
            user = null;
        } else {
            if(user.getPwd().equals("")){
                result="密码输入不合法";
            }else if(user.getName().equals("")){
                result="用户名输入不合法";
            }else{
                int i = userMapper.register(user);
                result = i == 1 ? "注册成功" : "注册失败";
            }
        }
        return result;
    }

    //获取所有用户
    public Page<User> getAllUser(int pageNum,int pageSize){
        Page<User> page = new Page(pageNum, pageSize);
        int count = userMapper.countByPage();
        int parameter = (pageNum-1)*pageSize;
        List<User> u = userMapper.getAllUser(parameter, pageSize);
        page.setList(u);
        page.setTotalCount(count);
        return page;
    }


}
